package com.ref.entity;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**
 * @author wzm
 * @create 2023-02-14 10:08
 */
public class ReflectTest5 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        //获取字节码文件对象
        Class clazz = Class.forName("com.ref.entity.Student");

        //获取name属性
        Field name = clazz.getDeclaredField("name");
        //修改临时访问访问权限
        name.setAccessible(true);

        //创建一个有属性的对象
        Student student = new Student();
        student.setName("张三");
        student.setAge(15);

        //设置属性的值 参数1：要修改谁的name  参数2：修改为多少
        name.set(student,"李四");
        System.out.println("student = " + student);
        
        //获取name值 参数表示我要获取这个对象xx属性
        Student student1 = new Student();
        student1.setName("王五");
        student1.setAge(48);
        String o = (String) name.get(student1);
        System.out.println("o = " + o);


    }
}
